#include <dbg.h>

#include <cassert>
#include <numeric>

using namespace std;

class Solution {
public:
    int maximumUniqueSubarray(vector<int>& nums) {
        int res = 0;
        int left = 0;
        int sum = 0;
        unordered_map<int, int> cnt;
        for (int right = 0; right < nums.size(); ++right) {
            ++cnt[nums[right]];
            sum += nums[right];
            while (cnt[nums[right]] > 1) {
                --cnt[nums[left]];
                sum -= nums[left];
                ++left;
            }
            res = max(res, sum);
        }
        return res;
    }
};

int main() {
    Solution s;
    vector<int> nums = {4, 2, 4, 5, 6};
    vector<int> nums2 = {5, 2, 1, 2, 5, 2, 1, 2, 5};
    assert(s.maximumUniqueSubarray(nums) == 17);
    assert(s.maximumUniqueSubarray(nums2) == 8);
    return 0;
}